package com.lanchetech.admin.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.JobApplyService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.JobApplyVo;
import com.lanchetech.dao.JobApplyMapper;
import com.lanchetech.entity.Job;
import com.lanchetech.entity.JobApply;
import com.lanchetech.entity.User;
import com.lanchetech.service.DaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class JobApplyServiceImpl implements JobApplyService {

    @Autowired
    JobApplyMapper jobApplyMapper;

    @Autowired
    DaoService daoService;

    @Override
    public ResultData<BasePageResp<JobApplyVo>> getJobApplyPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<JobApplyVo> list = jobApplyMapper.findAllPage(req);
        if(!CollectionUtils.isEmpty(list)){
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(JobApplyVo::getUserId).collect(Collectors.toList()));
            Map<Long, Job> jobMap= daoService.getJobMap(list.stream().map(JobApplyVo::getJobId).collect(Collectors.toList()));
            list.stream().forEach(item->{
                item.setUserNickname(userMap.get(item.getUserId()).getNickname());
                item.setJobTitle(jobMap.get(item.getJobId()).getTitle());
                item.setAvatar(userMap.get(item.getUserId()).getAvatar());
            });
        }
        return PageHelpUtil.buildPage(list,page);
    }
}
